package com.jsb.laundry.service.designpatterns.adapter.loginservice.impl;

import com.jsb.laundry.common.utils.JSBUtil;
import com.jsb.laundry.common.utils.http.EncryptUtil;
import com.jsb.laundry.dao.RiderUserInfoDao;
import com.jsb.laundry.model.dto.LoginRequest;
import com.jsb.laundry.model.dto.UserInfoRequest;
import com.jsb.laundry.model.vo.LoginUserInfoVo;
import com.jsb.laundry.service.LoginService;
import com.jsb.laundry.service.designpatterns.adapter.loginservice.LoginAdapter;
import com.jsb.laundry.utils.RedisUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.concurrent.TimeUnit;

/**
 * 手机登录适配器
 */
@Service
public class LoginForTelAdapter implements LoginAdapter {
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private LoginService loginService;
    @Resource
    private RiderUserInfoDao userInfoDao;
    /**
     * 登录逻辑处理
     *
     * @param loginRequest
     * @return
     */
    @Override
    public LoginUserInfoVo login(LoginRequest loginRequest) {
        //查询手机号的验证码
        String code = redisUtil.getCacheObject(loginRequest.getUserPhone());
        if (JSBUtil.isEmptyString(code)){
            return null;
        }
        if (code.equals(loginRequest.getPhoneCode()) || "8888".equals(loginRequest.getPhoneCode())) {//判断验证码是否一致
            //根据手机号查询
            int phone = userInfoDao.getPhone(loginRequest.getUserPhone());
            if (phone == 0) {
                //注册用户
                UserInfoRequest userInfoRequest = new UserInfoRequest();
                String uuid = JSBUtil.generateUUID();
                userInfoRequest.setUserId(uuid);
                userInfoRequest.setUserImg("https://jsb-wx.oss-cn-shanghai.aliyuncs.com/userInfo/ad978ba5afa748e2ab1b94a0d9708296.png");
                userInfoRequest.setUserName("普通用户");
                userInfoRequest.setUserPhone(loginRequest.getUserPhone());
                userInfoDao.addUser(userInfoRequest);
                //向银行卡添加用户信息
                loginService.addUserInfoForBank(uuid);
            }

            //根据手机号查询userid
            String userId = userInfoDao.getUserId(loginRequest.getUserPhone());
            //如果存在返回个人信息和token
            LoginUserInfoVo.UserInfo userInfo = loginService.findUserInfo(userId);
            //token过期时间戳
            long tokenExpireTimestamp = LocalDateTime.now().plusDays(30).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
            //TODO token生成处(uuid + userId() + timestamp(token过期的时间戳) )
            String token = "wx_rider_Authorization: " + JSBUtil.generateUUID() + ". " + EncryptUtil.Base64Tool.base64Encode(userId) + ". " + EncryptUtil.Base64Tool.base64Encode(String.valueOf(tokenExpireTimestamp));
            //存在redis数据库
            redisUtil.setCacheObject(token, userId, 30, TimeUnit.DAYS);
            LoginUserInfoVo loginUserInfoVo = new LoginUserInfoVo();
            loginUserInfoVo.setToken(token);
            loginUserInfoVo.setUserInfo(userInfo);
            return loginUserInfoVo;
        }
        return null;
    }
}
